local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();
local InstanceManager = CommonOHOH.load.InstanceManager();
local Mtool=require(script.Parent.Parent.tool)

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.Humanoid=self.parent:GetHumanoid()
    self.Battle=self.parent.Battle

    self.Att2Player={} --记录跟随点位到玩家的映射关系

    --进入追击状态
    CallBackManager:AddRbxConnect(self,  self.parent.ChaseTargetEvent, function(obj)
        -- print("进入追击状态")
        self:FollowUpdate()
    end);


    --死亡停止追击
    CallBackManager:AddRbxConnect(self, self.parent.DieEvent, function(obj)
        self:clear()
    end);

        

end;

-- 跟随追踪
function M.FollowUpdate(self)
    if self.FollowUpdateCon then
            return 
    end

    self.FollowUpdateCon=self:AddStepUpdate(function()
        local att=self:GetTagAtt()
        if att then
            self.Humanoid:MoveTo(att.WorldCFrame.Position)
            -- print("--------self.Humanoid------WalkSpeed----:",self.Humanoid.WalkSpeed)
        end
    end)
end

-- 更新目的地
function M.GetTagAtt(self)

    if self.Att then --已经存在跟随点位
        if self.Battle:IsHadPlayerInst(self.Att2Player[self.Att].main_inst) then --该玩家还存在
            return self.Att
        else --玩家已经离开，更新跟随点位
            self:UpdNowAtt()
        end

    else--不存在，创建一个
        self.Att=self:CreateFollowAtt()
    end
    return self.Att
end

-- 更新当前跟随点位
function M.UpdNowAtt(self)
    self.Att2Player[self.Att]=nil
    self.Att:Destroy()
    self.Att=self:CreateFollowAtt()
end

--创建一个跟随点位
function M.CreateFollowAtt(self)

    local playerList=self.Battle:GetAllPlayerList()
   local player= Mtool.FindClosestDistancePlayer(self.parent,playerList)

   if player then
        local att= self:SubCreateFollowAtt(player)
        self.Att2Player[att]=player
        return att
   end
 end


 
-- 根据player 随机生成跟随点位
function M.SubCreateFollowAtt(self,player)
    local shiled=player.character:GetPrivateComponent("Shield")
    local pos= shiled:GetFollowPos()
    local att=Instance.new("Attachment",player.character:GetRootPart_Loop())
    att.WorldCFrame=CFrame.new(pos)
    att.Name="FollowAtt"
    return att
end

--清楚跟踪信息
function M.clear(self)
    if self.FollowUpdateCon then
        self.FollowUpdateCon:Disconnect()
        self.FollowUpdateCon=nil
    end
    for k,v in pairs(self.Att2Player) do
        k:Destroy()
    end
    self.Att2Player={}
end


-- -- 开始追击
-- function M.FollowPlayer(self)

--     self:StopFollowPlayer()

--     self.TagPlayer=self.Battle:FindFollowPlayer(self.parent)

--     if  self.TagPlayer then

--         local shiled=self.TagPlayer.character:GetPrivateComponent("Shield")
--         local pos= shiled:GetFollowPos()
--         local att=Instance.new("Attachment",self.TagPlayer.character:GetRootPart_Loop())
--         att.WorldCFrame=CFrame.new(pos)
--         att.Name="f"
--         self.Att=att
--         self.UpdCon=self:AddStepUpdate(function()
--             self.Humanoid:MoveTo(self.Att.WorldCFrame.Position)
--         end)
--     else
--         print("找不到追击目标了")
--     end
-- end


-- -- 停止追击
-- function M.StopFollowPlayer(self)
--     if self.UpdCon then
--         self.UpdCon:Disconnect()
--         self.UpdCon=nil
--     end
--     self.TagPlayer=nil
    
--     if self.Att then
--         self.Att:Destroy()
--         self.Att=nil
--     end

--     self.parent:CloseMove()
-- end

function M.dtor(self)
    self:clear()
    M.super.dtor(self);
end

return M